import types from './types'
import lodash from 'lodash'

export default {
  [types.SET_IMAGES] (state, payload) {
    state.images = payload
  },
  [types.SET_IMAGE] (state, payload) {
    state.image = payload
  },
  [types.ADD_IMAGE] (state, payload) {
    state.images.items.push(payload)
    state.images.total ++

    state.imageList.push(payload)
  },
  [types.DELETE_IMAGE] (state, id) {
    let index = lodash.findIndex(state.images.items, ['id', id])
    if (index >= 0) {
      state.images.items.splice(index, 1)
    }

    index = lodash.findIndex(state.imageList, ['id', id])
    if (index >= 0) {
      state.imageList.splice(index, 1)
    }
  },
  [types.SET_IMAGE_LIST] (state, payload) {
    state.imageList = payload
  },
  [types.UPDATE_IMAGE] (state, payload) {
    let image = lodash.find(state.imageList, ['id', payload.id])
    if (image) {
      image = payload
    } else {
      state.imageList.push(payload)
    }
  }
}
